package main.gui.itemsdialog;

import java.awt.Dimension;
import java.awt.Font;
import java.util.Locale;

import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JTextField;

import main.core.Item;
import main.gui.utils.PriceDecimalFormatFactory;

public class SetItemData extends JPanel{

	private static final long serialVersionUID = 5886932912785103165L;
	private static final int TEXTFIELD_HEIGHT = 25;
	private static final int NAME_TEXTFIELD_WIDTH = 150;
	private static final int PRICE_TEXTFIELD_WIDTH = 50;
	
	private JTextField m_name;
	private JFormattedTextField m_start_price;
	private JFormattedTextField m_minimum_price;
	private JFormattedTextField m_maximum_price;
	
	public SetItemData(boolean header)
	{
		BoxLayout itemData = new BoxLayout(this, BoxLayout.X_AXIS);
		this.setLayout(itemData);
		
		
		//Textfield to fill in the name of the item
		m_name = new JTextField();
		m_name.setPreferredSize(new Dimension(NAME_TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT));
		if(header) { m_name.setText("Name"); m_name.setEditable(false); m_name.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12)); m_name.setBorder(javax.swing.BorderFactory.createEmptyBorder()); }
		this.add(m_name);

		//Textfield to fill in the start price of the item
		m_start_price = new JFormattedTextField(PriceDecimalFormatFactory.getDecimalFormat("0.00", new Locale("fr", "FR")));
		m_start_price.setPreferredSize(new Dimension(PRICE_TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT));
		if(header) { m_start_price.setText("Start"); m_start_price.setEditable(false); m_start_price.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12)); m_start_price.setBorder(javax.swing.BorderFactory.createEmptyBorder()); }
		this.add(m_start_price);

		//Textfield to fill in the minimum price of the item
		m_minimum_price = new JFormattedTextField(PriceDecimalFormatFactory.getDecimalFormat("0.00", new Locale("fr", "FR")));
		m_minimum_price.setText("0,65");
		m_minimum_price.setPreferredSize(new Dimension(PRICE_TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT));
		if(header) { m_minimum_price.setText("Min"); m_minimum_price.setEditable(false); m_minimum_price.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12)); m_minimum_price.setBorder(javax.swing.BorderFactory.createEmptyBorder()); }
		this.add(m_minimum_price);

		//Textfield to fill in the maximum price of the item
		m_maximum_price = new JFormattedTextField(PriceDecimalFormatFactory.getDecimalFormat("0.00", new Locale("fr", "FR")));
		m_maximum_price.setText("2,00");
		m_maximum_price.setPreferredSize(new Dimension(PRICE_TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT));
		if(header) { m_maximum_price.setText("Max"); m_maximum_price.setEditable(false); m_maximum_price.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12)); m_maximum_price.setBorder(javax.swing.BorderFactory.createEmptyBorder()); }
		this.add(m_maximum_price);
	}

	public String name()
	{
		return this.m_name.getText();
	}
	
	public String start_price()
	{
		return this.m_start_price.getText();
	}
	
	public String minimum_price()
	{
		return this.m_minimum_price.getText();
	}
	
	public String maximum_price()
	{
		return this.m_maximum_price.getText();
	}

	public void setName(String name)
	{
		this.m_name.setText(name);
	}
	
	public void setStart_price(String start_price)
	{
		this.m_start_price.setText(start_price);
	}
	
	public void setMinimum_price(String minimum_price)
	{
		this.m_minimum_price.setText(minimum_price);
	}
	
	public void setMaximum_price(String maximum_price)
	{
		this.m_maximum_price.setText(maximum_price);
	}
	
	public Item getItem()
	{
		Item item = null;
		if(!this.name().equals(""))
		{
			item = new Item();
			item.setName(this.name());
			item.setMaximum_price(this.maximum_price().replace(',', '.'));
			item.setMinimum_price(this.minimum_price().replace(',', '.'));
			item.setCurrent_price(this.start_price().replace(',', '.'));
		}
		return item;
	}

	public void setItem(Item item)
	{
		this.setName(item.name());
		this.setMaximum_price("" + item.maximum_price());
		this.setMinimum_price("" + item.minimum_price());
		this.setStart_price("" + item.current_price());
	}
	
}
